﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AstekServis.Web.Data;
using System.ServiceModel.DomainServices.Client;
using DevExpress.AgMenu;

namespace AstekServis.Views
{
    public partial class BilgiEkrani : ChildWindow
    {
        AsTekDomainContext context = new AsTekDomainContext();
        public string AracKodu { get; set; }
        public int AracID { get; set; }
        public string FirmaAdi { get; set; }
        public string GrupAdi { get; set; }
        public string Adres { get; set; }

        public BilgiEkrani()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(BilgiEkrani_Loaded);
        }
        void BilgiEkrani_Loaded(object sender, RoutedEventArgs e)
        {
            lblHeader.Content = "";
            if (!string.IsNullOrEmpty(AracKodu))
            {
                OzellikListesi();
            }
        }

        private void MontajListesi(string arackod)
        {
            EntityQuery<MontajListe> qry = context.GetMontajListesiQuery().Where(x => x.AracKodu == arackod);
            LoadOperation<MontajListe> operation = context.Load(qry);
            grdMontaj.DataSource = null;
            grdMontaj.Columns.Clear();
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<MontajListe> op = ss as LoadOperation<MontajListe>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    grdMontaj.AddColumn("Firma");
                    grdMontaj.AddColumn("Grup");
                    grdMontaj.AddColumn("Plaka");
                    grdMontaj.AddColumn("Fatura");
                    grdMontaj.AddColumn("Tarih");
                    grdMontaj.DataSource = op.Entities;
                }
            };
        }
        private void ServisListesi(int aracID)
        {
            EntityQuery<ServisListe> qry;
            qry = context.GetServisListesiQuery().Where(x => x.AracID == aracID);
            LoadOperation<ServisListe> operation = context.Load(qry);
            grdServis.DataSource = null;
            grdServis.Columns.Clear();

            operation.Completed += (ss, oo) =>
            {
                LoadOperation<ServisListe> op = ss as LoadOperation<ServisListe>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    grdServis.AddColumn("Firma");
                    grdServis.AddColumn("Plaka");
                    grdServis.AddColumn("ServiseGidilecekTarih");
                    grdServis.AddColumn("Sorunlar");
                    grdServis.AddColumn("Teknisyenler");
                    grdServis.AddColumn("Servis");
                    grdServis.AddColumn("Durum");
                    grdServis.AddColumn("Grup");
                    grdServis.DataSource = op.Entities;
                }
            };
        }

        private void MarkaListesi(int markaID)
        {
            EntityQuery<AracMarka> qry = context.GetAracMarkalariQuery().Where(x => x.ID == markaID);
            LoadOperation<AracMarka> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<AracMarka> op = ss as LoadOperation<AracMarka>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    AracMarka marka = op.Entities.FirstOrDefault();
                    if (marka != null)
                    {
                        txtMarka.Text = marka.MarkaAdi;
                    }
                }
            };
        }
        private void OzellikListesi()
        {
            EntityQuery<OzellikTanimi> qry = context.GetOzellikTanimlariQuery();
            LoadOperation<OzellikTanimi> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<OzellikTanimi> op = ss as LoadOperation<OzellikTanimi>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    grdEkBilgiler.AddColumn("OzellikAdi");
                    grdEkBilgiler.Columns[0].Width = 200;
                    grdEkBilgiler.Columns[0].AllowEditing = DevExpress.Utils.DefaultBoolean.False;
                    grdEkBilgiler.AddColumn("Değer");
                    grdEkBilgiler.Columns[1].AllowEditing = DevExpress.Utils.DefaultBoolean.True;
                    grdEkBilgiler.Columns[1].UnboundType = DevExpress.Data.UnboundColumnType.String;

                    grdEkBilgiler.DataSource = op.Entities;

                    for (int i = 0; i < grdEkBilgiler.RowCount; i++)
                    {
                        grdEkBilgiler.SetCellValue(i, grdEkBilgiler.Columns["Değer"], "");
                    }

                    LoadArac();
                }
            };
        }
        private void LoadFirmware(int firmwareID)
        {
            EntityQuery<FirmwareTanim> qry = context.GetFirmwareTanimlariQuery().Where(x => x.ID == firmwareID);
            LoadOperation<FirmwareTanim> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<FirmwareTanim> op = ss as LoadOperation<FirmwareTanim>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    FirmwareTanim firm = op.Entities.FirstOrDefault();
                    if (firm != null)
                    {
                        txtFirmware.Text = firm.Aciklama;
                    }
                }
            };
        }
        private void LoadSistem(int sistemID)
        {
            EntityQuery<SistemTanim> qry = context.GetSistemTanimlariQuery().Where(x => x.ID == sistemID);
            LoadOperation<SistemTanim> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<SistemTanim> op = ss as LoadOperation<SistemTanim>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    SistemTanim sis = op.Entities.FirstOrDefault();
                    if (sis != null)
                    {
                        txtSistem.Text = sis.Aciklama;
                    }
                }
            };
        }

        private void LoadArac()
        {
            EntityQuery<Arac> qry = context.GetAraclarQuery().Where(x => x.AracKodu == AracKodu);
            LoadOperation<Arac> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<Arac> op = ss as LoadOperation<Arac>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    Arac arc = op.Entities.FirstOrDefault();
                    if (arc != null)
                    {
                        AracID = arc.ID;

                        LoadFirmware(arc.FirmwareID);
                        MarkaListesi(arc.MarkaID);
                        LoadSistem(arc.SistemID);

                        txtAdres.Text = Adres;
                        txtFirma.Text = FirmaAdi;
                        txtGrup.Text = GrupAdi;
                        txtKoltukSayisi.Text = arc.KoltukSayisi.ToString();
                        txtMontajTarihi.Text = arc.MontajTarihi.ToShortDateString();
                        txtMontajYeri.Text = arc.MontajYeri;
                        txtPlaka.Text = arc.Plaka;
                        txtSasiNo.Text = arc.SasiNo;
                        txtSistemNo.Text = arc.SistemNo;
                        txtUretimYili.Text = arc.UretimYili.Year.ToString();

                        AracOzellikLoad();

                        MontajListesi(arc.AracKodu);
                        ServisListesi(arc.ID);

                        new Methods().garantiSozlemesiBul(arc.ID, lblHeader, arc.MontajTarihi);
                    }
                }
            };
        }
        private void AracOzellikLoad()
        {
            EntityQuery<AracOzellik> qry = context.GetAracOzellikleriQuery().Where(x => x.AracID == AracID);
            LoadOperation<AracOzellik> operation = context.Load(qry);
            operation.Completed += (ss, oo) =>
            {
                LoadOperation<AracOzellik> op = ss as LoadOperation<AracOzellik>;
                if (op.HasError)
                {
                    System.Windows.MessageBox.Show(op.Error.ToString(), "Load Error",
                    System.Windows.MessageBoxButton.OK);
                    op.MarkErrorAsHandled();
                }
                else
                {
                    foreach (AracOzellik item in op.Entities)
                    {
                        for (int i = 0; i < grdEkBilgiler.RowCount; i++)
                        {
                            OzellikTanimi tnm = (OzellikTanimi)grdEkBilgiler.GetDataRow(i);
                            if (tnm != null)
                            {
                                if (tnm.ID == item.OzellikID)
                                {
                                    grdEkBilgiler.SetCellValue(i, grdEkBilgiler.Columns["Değer"], item.Deger);
                                }
                            }
                        }
                    }
                }
            };
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void btnServisAc_Click(object sender, RoutedEventArgs e)
        {
            yeniServis frm = new yeniServis();
            object row = grdServis.FocusedDataRow;
            if (row != null)
            {
                ServisListe yeni = (ServisListe)row;
                frm.ServisID = yeni.ID;
            }
            frm.Show();
        }

        private void btnMontajIncele_Click(object sender, RoutedEventArgs e)
        {
            AracTanitim frm = new AracTanitim();
            frm.YeniArac = false;
            object row = grdMontaj.FocusedDataRow;
            if (row != null)
            {
                MontajListe yeni = (MontajListe)row;
                frm.MontajBilgi = new MontajBilgi()
                {
                    Adres = yeni.Adres,
                    BelgeNo = yeni.BelgeNo,
                    FirmaAdi = yeni.Firma,
                    GrupAdi = yeni.Grup,
                    Tarih = yeni.Tarih,
                    ID = yeni.ID,
                    AltCariKodu = yeni.AltCariKodu,
                    AnaCariKodu = yeni.AnaCariKodu,
                    AracKodu = yeni.AracKodu,
                    FaturaID = yeni.FaturaID
                };
            }
            frm.Show();
        }
    }
}

